--[[
    NuckPluck - An android game for learning the Thai alphabet
    Copyright (C) 2013 Nathan Kieffer

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.

    Contact: nathankieffer@gmail.com
--]]

local physics = require("physics")
physics.start()
local filename = {
   ["ก"] = "0.mp3",
   ["ข"] = "1.mp3",
   ["จ"] = "7.mp3"
}
local Letter = {}

function Letter.newLetter(parent, char, x, y, timeLimit)
   local letter = display.newGroup()
   local container = display.newCircle(letter, 0,0, 12.5)
   container:setFillColor(.7,0,0)
   letter.char = display.newText(letter, char, 0,0,native.systemFont, 24)
   parent:insert(letter)
   letter.x = (x * 50)
   letter.y = (y * 50)
   letter.startx = (x * 50)
   letter.starty = (y * 50)
   letter:toFront()
   letter.type="letter"
   letter.subtype=char
   physics.addBody(letter, "dynamic", {isSensor = false, density=0, 
				      friction=0, bounce=1,
				      radius=12.5,
				      filter={ categoryBits=16,
					       maskBits=125} })
   letter.type = "letter"

   function letter:collision(event)
      if event.other.subtype == "air" then
	 self.alpha=.5
	 transition.to(self, {x=self.startx, y=self.starty,time=400,
			      onComplete=function(event)
				 self.alpha=1
			      end})
      end
   end

   letter:addEventListener("collision", letter)
   return letter
end

function Letter.newGoal(parent, char,x,y)
   
   local goal = display.newGroup()
   goal.green = display.newImage(goal, "images/tile_goal_green.png")
   goal.red = display.newImage(goal,"images/tile_goal_red.png")
   goal.red.isVisible = false
   goal.sfx = audio.loadSound("sounds/"..filename[char])
   goal.char = char
   parent:insert(goal)
   goal.x = x*50
   goal.y = y*50
   
   function goal:collision(event)
      self.green.isVisible = false
      self.red.isVisible = true
      if not self.playing then
	 self.playing = true
	 audio.play(self.sfx, { onComplete=function(event) self.red.isVisible = false self.green.isVisible = true self.playing = false end})
      end
      local letter = event.other
      if letter.type == "letter" then
	 if letter.char.text == self.char then
	    local correct = letter.parent.correct
	    correct = correct + 1
	    letter.parent.correct = correct
	    self:removeEventListener("collision", self)
	    transition.to(self, {xScale=20, yScale=20, alpha=0, onComplete=function(event)
				    Runtime:removeEventListener("enterFrame", self)
				    self:removeSelf()
								  end})
	    letter:removeEventListener("collision", event.other)
	    transition.to(letter, {x=math.random(-20,0),y=math.random(-20,0)})

	 else
	    letter.alpha = .5
	    transition.to(letter, {x=letter.startx, y=letter.starty,time=400,
			      onComplete=function(event)
				 letter.alpha=1
			      end})
	 end	    
      end
   end
   physics.addBody(goal, "dynamic", {isSensor = true, density=0, 
				      friction=0, bounce=0,
				      filter={ categoryBits=64,
					       maskBits=17} })
   
   goal:addEventListener("collision", goal)
   Runtime:addEventListener("enterFrame", goal)
   return goal
end

return Letter
